<?php
// +----------------------------------------------------------------------
// | title: 每日在线数据统计
// +----------------------------------------------------------------------
// | Author: TZ.Leong
// +----------------------------------------------------------------------
// | Date: 2019/7/6 18:04
// +----------------------------------------------------------------------
// | RUN: php think DayOnline mxwh5 single --debug 1 --s_date 2019-07-01 --e_date 2019-07-01
// +----------------------------------------------------------------------

namespace app\single\command;


use app\single\model\DayOnlineModel;
use app\single\model\PayLogModel;
use app\single\model\RoleModel;
use think\Command;
use think\facade\Cache;

class DayOnline extends Command
{
    protected $minus = "";

    /**
     * @param \think\console\Input $input
     * @param \think\console\Output $output
     */
    protected function execute($input, $output)
    {
        $roleModel = new RoleModel();
        $payLogModel = new PayLogModel();
        //统计整形时间
        $date = date("Ymd", strtotime($this->sDate));
        //当前分钟
        $minus = date("H:i");
        
        $currentTimestamp = time();

        // 计算最近 5 分钟的整数倍时间戳
        $alignedTimestamp = $currentTimestamp - ($currentTimestamp % 300);
        $this->minus = date('H:i', $alignedTimestamp);
        $this->minus = ($minus === "00:00") ? "24:00" : $minus;
        //当前在线数据
        $onlineInfo = $roleModel->getCurrentOnline([["is_online","=", 1]]);
//        $onlineInfo = 0;
        //当天注册玩家
//        $onlineNewInfo = $roleModel->getCurrentOnline([
//            ["online_flag","=",1],
//            ["reg_time","between",[strtotime($this->sDate." 00:00:00"),strtotime($this->eDate." 23:59:59")]]
//        ]);
        //此刻充值次数，充值金额，充值人数
        $payData = $payLogModel->statPay([
            ["pay_date", "=", $date],
        ]);
        //当月1号到此刻充值金额，次数，人数
        $payMonthData = $payLogModel->statPay([
            ["pay_date", "between", [date("Ym01", strtotime($this->sDate)), $date]],
        ]);
        //此刻登陆用户数
        $loginData = $roleModel->getCurrentOnline([
            ["last_login_time", "between", [strtotime($this->sDate . " 00:00:00"), strtotime($this->eDate . " 23:59:59")]],
        ]);
        //此刻创角人数
        $regData = $roleModel->getCurrentOnline([
            ["reg_time", "between", [strtotime($this->sDate . " 00:00:00"), strtotime($this->eDate . " 23:59:59")]],
        ]);
        //获取本服所有平台
        $platArr = getPlatformLists();
        echo "platArr:" . json_encode($platArr, true) . "\n";
        //开始入库
        echo "debug somethings:" . json_encode(["online" => $onlineInfo, "payData" => $payData, "loginData" => $loginData,
                "regData" => $regData, "payMonthData" => $payMonthData]) . "\n";
        dump($onlineInfo);
        foreach ($platArr as $platform) {
            if (isset($onlineInfo[$platform])) {
                //开始更新插入全部玩家数据
                echo "开始更新插入全部玩家数据".$platform.$onlineInfo[$platform]["num"]."\n";
                $this->insertData($platform, 1, 0, $onlineInfo[$platform]["num"]);
            }
//            //开始更新插入新玩家数据
//            $this->insertData(1,1,$onlineNewInfo[$platform]["num"],$onlineNewInfo[$platform]["ip_num"],$platform,$obj->sDate,$minus);
            if (isset($payData[$platform])) {
                //开始更新插入充值金额数据
                $this->insertData($platform, 2, 0, $payData[$platform]["rmb"]);
                //开始更新插入充值次数数据
                $this->insertData($platform, 3, 0, $payData[$platform]["num"]);
                //开始更新插入充值人数数据
                $this->insertData($platform, 4, 0, $payData[$platform]["players"]);
            }
            if (isset($loginData[$platform])) {
                //开始更新插入登陆人数数据
                $this->insertData($platform, 5, 0, $loginData[$platform]["num"]);
            }
            if (isset($regData[$platform])) {
                //开始更新插入创角人数数据
                $this->insertData($platform, 6, 0, $regData[$platform]["num"]);
            }
            if (isset($payMonthData[$platform])) {
                //开始更新插入当前月总充值金额
                $this->insertData($platform, 7, 0, $payMonthData[$platform]["rmb"]);
            }
        }
    }

    /**
     * 更新和添加数据
     * @param string $platform
     * @param int $project 统计项目1-在线；2-充值金额；3-充值次数；4-充值人数；5-登陆用户数；6-创角人数
     * @param int $type 玩家类型0-所有玩家；1-老玩家
     * @param int $num
     */
    public function insertData($platform, $project, $type, $num)
    {
        $day = date("Ymd", strtotime($this->sDate));
        $model = new DayOnlineModel();
        //写入或更新当前在线数据
        $checkInfo = $model->field('id,online_txt')
            ->where(array('stat_project' => $project, 'platform' => $platform, "type" => $type, 'day' => $day))
            ->find();
        //修改逻辑
        if ($checkInfo['id'] > 0) {
            $add['online_txt'] = unserialize($checkInfo['online_txt']);
            $add['online_txt'][$this->minus] = (int)$num;//加入当前时间的在线人数
            $add['max_num'] = max($add['online_txt']);
            $add['min_num'] = min($add['online_txt']);
            $add['avg_num'] = (int)(array_sum($add['online_txt']) / count($add['online_txt']));
            $add['online_txt'] = serialize($add['online_txt']);
            $add['id'] = $checkInfo['id'];
            $model->update($add);
        } //添加
        else {
            $add["type"] = $type;
            $add["stat_project"] = $project;
            $add['day'] = $day;
            $add['platform'] = $platform;
            $add['online_txt'][$this->minus] = (int)$num;//加入当前时间的在线人数
            $add['min_num'] = $add['max_num'] = $add['avg_num'] = (int)$num;
            $add['online_txt'] = serialize($add['online_txt']);
            $model->insert($add);
        }
        echo "add;".json_encode($add)."\n";
    }
}